package fr.utilities;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public final class DateUtil {
	
	private DateUtil(){}
	
	public static Date stringToDate(String dateString,String pattern) 
			throws ParseException{
		final DateFormat df = new SimpleDateFormat(pattern);
		return df.parse(dateString);
	}

	public static String dateToString(Date date,String pattern){
		final DateFormat df = new SimpleDateFormat(pattern);
		return df.format(date);
	}

	public static boolean compareDateIgnoreTime(Date date1,Date date2){
		if(date1 == null){
			return false;
		}
		else if(date2 == null){
			return false;
		}else{
			 return dateToString(date1,"yyyyMMdd").equals(dateToString(date2,"yyyyMMdd"));
		}
		
	}

	public static String getDateView(Date date){

		StringBuffer ret; 
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, -1);

		Calendar calHier = Calendar.getInstance();
		calHier.setTime(new Date());
		calHier.add(Calendar.DAY_OF_MONTH, -1);

		if(compareDateIgnoreTime(new Date(),date)){
			ret = new StringBuffer("Aujourd'hui");
		}
		else if(compareDateIgnoreTime(calHier.getTime(),date)){
			ret = new StringBuffer("Hier");
		}
		else{
			ret = new StringBuffer(dateToString(date,"dd"));
			ret.append(" ");
			ret.append(cal.getDisplayName(Calendar.MONTH, Calendar.ALL_STYLES, Locale.FRANCE));
		}
		
		ret.append(" ");
		ret.append(dateToString(date,"HH:mm"));
		
		return ret.toString();
		
	}

	
	
}
